package com.raisecom.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileOper {

	public static void readFileByBytes(String fileName) {
		File file = new File(fileName);
		InputStream in = null;
		try {
			in = new FileInputStream(file);
			int tempbyte;
			while ((tempbyte = in.read()) != -1) {
				System.out.write(tempbyte);
			}
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		try {
			byte[] tempbytes = new byte[100];
			int byteread = 0;
			in = new FileInputStream(fileName);
			FileOper.showAvailableBytes(in);
			while ((byteread = in.read(tempbytes)) != -1) {
				System.out.write(tempbytes, 0, byteread);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public static void readFileByChars(String fileName) {
		File file = new File(fileName);
		Reader reader = null;
		try {
			reader = new InputStreamReader(new FileInputStream(file));
			int tempchar;
			while ((tempchar = reader.read()) != -1) {
				if (((char) tempchar) != '\r') {
					System.out.print((char) tempchar);
				}
			}
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			char[] tempchars = new char[30];
			int charread = 0;
			reader = new InputStreamReader(new FileInputStream(fileName));
			while ((charread = reader.read(tempchars)) != -1) {
				if ((charread == tempchars.length)
						&& (tempchars[tempchars.length - 1] != '\r')) {
					System.out.print(tempchars);
				} else {
					for (int i = 0; i < charread; i++) {
						if (tempchars[i] == '\r') {
							continue;
						} else {
							System.out.print(tempchars[i]);
						}
					}
				}
			}

		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public static String readFileByLinesBuf(String fileName) {
		File file = new File(fileName);
		StringBuilder builder = new StringBuilder();

		BufferedReader reader = null;
		try {
			// System.out.println("锟斤拷锟斤拷为锟斤拷位锟斤拷取锟侥硷拷锟斤拷锟捷ｏ拷一锟轿讹拷一锟斤拷锟叫ｏ拷");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一锟轿讹拷锟斤拷一锟叫ｏ拷直锟斤拷锟斤拷锟斤拷null为锟侥硷拷锟斤拷锟斤拷
			while ((tempString = reader.readLine()) != null) {
				// 锟斤拷示锟叫猴拷
				// System.out.println("line " + line + ": " + tempString);
				builder.append(tempString);
				line++;
			}
			reader.close();

			return builder.toString();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return null;
	}

	/**
	 * 锟斤拷锟斤拷为锟斤拷位锟斤拷取锟侥硷拷锟斤拷锟斤拷锟斤拷锟节讹拷锟斤拷锟斤拷锟叫的革拷式锟斤拷锟侥硷拷
	 */
	public static void readFileByLines(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("锟斤拷锟斤拷为锟斤拷位锟斤拷取锟侥硷拷锟斤拷锟捷ｏ拷一锟轿讹拷一锟斤拷锟叫ｏ拷");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一锟轿讹拷锟斤拷一锟叫ｏ拷直锟斤拷锟斤拷锟斤拷null为锟侥硷拷锟斤拷锟斤拷
			while ((tempString = reader.readLine()) != null) {
				// 锟斤拷示锟叫猴拷
				System.out.println("line " + line + ": " + tempString);
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	/**
	 * 锟斤拷锟斤拷取锟侥硷拷锟斤拷锟斤拷
	 */
	public static void readFileByRandomAccess(String fileName) {
		RandomAccessFile randomFile = null;
		try {
			System.out.println("锟斤拷锟斤拷取一锟斤拷锟侥硷拷锟斤拷锟捷ｏ拷");
			// 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷只锟斤拷锟斤拷式
			randomFile = new RandomAccessFile(fileName, "r");
			// 锟侥硷拷锟斤拷锟饺ｏ拷锟街斤拷锟斤拷
			long fileLength = randomFile.length();
			// 锟斤拷锟侥硷拷锟斤拷锟斤拷始位锟斤拷
			int beginIndex = (fileLength > 4) ? 4 : 0;
			// 锟斤拷锟斤拷锟侥硷拷锟侥匡拷始位锟斤拷锟狡碉拷beginIndex位锟矫★拷
			randomFile.seek(beginIndex);
			byte[] bytes = new byte[10];
			int byteread = 0;
			// 一锟轿讹拷10锟斤拷锟街节ｏ拷锟斤拷锟斤拷募锟斤拷锟斤拷莶锟斤拷锟�0锟斤拷锟街节ｏ拷锟斤拷锟绞ｏ拷碌锟斤拷纸凇锟�
			// 锟斤拷一锟轿讹拷取锟斤拷锟街斤拷锟斤拷锟絙yteread
			while ((byteread = randomFile.read(bytes)) != -1) {
				System.out.write(bytes, 0, byteread);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (randomFile != null) {
				try {
					randomFile.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	/**
	 * 锟斤拷示锟斤拷锟斤拷锟斤拷锟叫伙拷剩锟斤拷锟街斤拷锟斤拷
	 */
	private static void showAvailableBytes(InputStream in) {
		try {
			System.out.println("锟斤拷前锟街斤拷锟斤拷锟斤拷锟斤拷锟叫碉拷锟街斤拷锟斤拷为:"
					+ in.available());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * A锟斤拷锟斤拷追锟斤拷锟侥硷拷锟斤拷使锟斤拷RandomAccessFile
	 */
	public static void appendMethodA(String fileName, String content) {
		try {
			// 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷写锟斤拷式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 锟侥硷拷锟斤拷锟饺ｏ拷锟街斤拷锟斤拷
			long fileLength = randomFile.length();
			// 锟斤拷写锟侥硷拷指锟斤拷锟狡碉拷锟侥硷拷尾锟斤拷
			randomFile.seek(fileLength);
			randomFile.writeBytes(content);
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * B锟斤拷锟斤拷追锟斤拷锟侥硷拷锟斤拷使锟斤拷FileWriter
	 */
	public static void appendMethodB(String fileName, String content) {
		try {
			// 锟斤拷一锟斤拷写锟侥硷拷锟斤拷锟斤拷锟斤拷锟届函锟斤拷锟叫的第讹拷锟斤拷锟斤拷锟斤拷true锟斤拷示锟斤拷追锟斤拷锟斤拷式写锟侥硷拷
			FileWriter writer = new FileWriter(fileName, true);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 锟斤拷锟斤拷募锟斤拷锟�
	 */
	public static void createXmlFile(String fileName, String content) {
		try {
			// 锟斤拷一锟斤拷写锟侥硷拷锟斤拷锟斤拷锟斤拷锟届函锟斤拷锟叫的第讹拷锟斤拷锟斤拷锟斤拷true锟斤拷示锟斤拷追锟斤拷锟斤拷式写锟侥硷拷
			FileWriter writer = new FileWriter(fileName, false);
			writer.write(content);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// public static void main(String[] args) {
	// String fileName = "C:/temp/newTemp.txt";
	// String content = "new append!";
	// //锟斤拷锟斤拷锟斤拷A追锟斤拷锟侥硷拷
	// FileOper.appendMethodA(fileName, content);
	// FileOper.appendMethodA(fileName, "append end. \n");
	// //锟斤拷示锟侥硷拷锟斤拷锟斤拷
	// FileOper.readFileByLines(fileName);
	// //锟斤拷锟斤拷锟斤拷B追锟斤拷锟侥硷拷
	// FileOper.appendMethodB(fileName, content);
	// FileOper.appendMethodB(fileName, "append end. \n");
	// //锟斤拷示锟侥硷拷锟斤拷锟斤拷
	// FileOper.readFileByLines(fileName);
	// }

	/**
	 * 锟侥硷拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
	 */
	public static String createFileName(Date date) {
		return String.valueOf(date.getTime());
	}

	/**
	 * 锟矫碉拷锟侥硷拷锟斤拷锟侥硷拷锟斤拷
	 */
	public static String getFileName(String path) {
		String[] strArray = path.split("\\/");
		return strArray[strArray.length - 1];
	}

	/**
	 * 锟矫碉拷锟侥硷拷锟斤拷锟侥硷拷锟斤拷 (锟斤拷锟斤拷锟斤拷锟斤拷缀锟斤拷)
	 */
	public static String getFileNameNoStuffix(String path) {
		// String[] strArray = path.split("\\/");
		String[] strArray = path.split("\\" + File.separator);
		String fileName = strArray[strArray.length - 1];
		String[] fileNameArray = fileName.split("\\.");
		return fileNameArray[0];
	}

	/**
	 * 锟矫碉拷锟侥硷拷锟侥猴拷缀锟斤拷
	 */
	public static String getFileSuffix(String path) {
		String[] strArray = path.split("\\/");
		String fileName = strArray[strArray.length - 1];
		String[] fileNameArray = fileName.split("\\.");
		return fileNameArray[fileNameArray.length - 1];
	}

	/**
	 * 锟矫碉拷锟斤拷前时锟斤拷 yyyy-MM-dd锟斤拷式
	 */
	public static String getTodayString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	// public static void main(String[] args) {
	// String fileName = "C:/temp/newTemp.txt";
	// FileOper.readFileByBytes(fileName);
	// FileOper.readFileByChars(fileName);
	// FileOper.readFileByLines(fileName);
	// FileOper.readFileByRandomAccess(fileName);
	// }

	public static void main(String[] args) {
		String str = "/opt/ftp/121212.upload";
		String fileName = getFileNameNoStuffix(str);
		System.out.println(fileName);

		String filesuffix = getFileSuffix(str);
		System.out.println(filesuffix);
	}
}
